package com.shen.demo.demos.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.shen.demo.demos.web.compont.webSocket.HttpAuthHandler;
import com.shen.demo.demos.web.domain.entry.UserName;
import com.shen.demo.demos.web.service.UserNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/myWS/user")
public class UserNameController {

    @Autowired
    private UserNameService userNameService;
    @Autowired
    HttpAuthHandler httpAuthHandler;

    // 查询所有用户
    @GetMapping
    public List<UserName> getAllUserNames() {
        return userNameService.list();
    }

    // 根据ID查询用户
    // 根据ID查询用户
    @GetMapping("/{id}")
    public ResponseEntity<UserName> getUserNameById(@PathVariable Integer id) {
        return userNameService.getById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    // 创建用户
    @PostMapping
    public UserName createUserName(@RequestBody UserName userName) {
        userNameService.save(userName);
        return userName;
    }

    // 更新用户
    @PutMapping("/{id}")
    public ResponseEntity<UserName> updateUserName(@PathVariable Integer id, @RequestBody UserName userName) {
        userName.setId(id);
        if (userNameService.updateById(userName)) {
            return ResponseEntity.ok(userName);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUserName(@PathVariable Long id) {
        if (userNameService.removeById(id)) {
            return ResponseEntity.status(200).build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    @GetMapping("/announce")
    public String sendAnnouncement() {
        try {
            // 模拟公告内容
            String announcement = "新公告发布于: " + System.currentTimeMillis();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("announcement", announcement);
            HttpAuthHandler.broadcast(jsonObject);
            System.out.println("公告已发布: " + jsonObject);
            return "公告已发布: " + jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发布公告失败");
            return "发布失败";
        }
    }
    @PostMapping("/demo/addUserName")
    public ResponseEntity<UserName> addCompany(String name) {
        UserName user = new UserName();
        user.setName(name);
        user.setAge(20); // 修改为字符串
        user.setGrade(4); // 修改为字符串
        boolean save = this.userNameService.updateById(user);
        return ResponseEntity.ok().build();
    }
    @PostMapping("/demo/updateUserName")
    public ResponseEntity<UserName> updateCompany(int id) {
        UserName user = this.userNameService.getById(id).orElseThrow(() -> new RuntimeException("记录不存在"));
        user.setName("theonefx");
        user.setGrade(2); // 修改为字符串
        user.setAge((new Random().nextInt(10))); // 修改为字符串
        this.userNameService.updateById(user);
        System.out.println("更新数据"+user);
        return ResponseEntity.ok().build();
    }


}
